/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package math.statistics;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author Shimu
 */
public class Graph2D {

    private int[] y_values;

    private int x_min, x_max;

    private int y_min, y_max;

    private int screenX, screenY;

    private final int x_margin = 10;

    public Graph2D(int[] y_values) {
        this.y_values = y_values;

        x_min = 0;
        x_max = y_values.length;
        screenX = x_max - x_min + (x_margin * 2);
        y_min = min(y_values);
        //y_max = max(y_values);
        //screenY = y_max - y_min;
        screenY = 320;
    }

    /*
    public Graph2D(int[] x_values, int[] y_values) {
    this.x_values = x_values;
    this.y_values = y_values;
    
    x_min = min(x_values);
    x_max = max(x_values);
    screenX = x_max - x_min;
    y_min = min(y_values);
    y_max = max(y_values);
    screenY = y_max - y_min;
    }
     * 
     */
    /** Returns the minimum integer in the given integer array */
    private Integer min(int[] nums) {
        if (nums.length == 0) {
            return null;
        }

        int min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] < min) {
                min = nums[i];
            }
        }

        return min;
    }

    public BufferedImage renderGraph(Color color, double maxValue) {
        BufferedImage screenBuffer = new BufferedImage(screenX, screenY,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) screenBuffer.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, screenX, screenY);
        g.setColor(color);
        double ratio = screenY / maxValue;


        for (int i = 0; i < y_values.length; i++) {
            g.drawLine(i + x_margin, screenY, i + x_margin,
                    screenY - (int) (ratio * y_values[i]));
            //screenY - (int)(screenY * y_values[i] / (float) y_max)
        }
        return screenBuffer;
    }
}
